package com.linkoog.devtools.generate.method_creater.domain;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class ControllerMethodVo {

    private String modelName;

    private String httpMethod;

    private String httpUrl;

    private  String apiName;

    private String apiRemark;

    private String methodVisibility;

    private String methodName;

    private String methodReturn;

    private Set<String> importSet = new HashSet<>();

    public String getModelName() {
        return modelName;
    }

    public void setModelName(String modelName) {
        this.modelName = modelName;
    }

    public String getHttpMethod() {
        return httpMethod;
    }

    public void setHttpMethod(String httpMethod) {
        this.httpMethod = httpMethod;
    }

    public String getHttpUrl() {
        return httpUrl;
    }

    public void setHttpUrl(String httpUrl) {
        this.httpUrl = httpUrl;
    }

    public String getApiName() {
        return apiName;
    }

    public void setApiName(String apiName) {
        this.apiName = apiName;
    }

    public String getApiRemark() {
        return apiRemark;
    }

    public void setApiRemark(String apiRemark) {
        this.apiRemark = apiRemark;
    }

    public String getMethodVisibility() {
        return methodVisibility;
    }

    public void setMethodVisibility(String methodVisibility) {
        this.methodVisibility = methodVisibility;
    }

    public String getMethodName() {
        return methodName;
    }

    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }

    public String getMethodReturn() {
        return methodReturn;
    }

    public void setMethodReturn(String methodReturn) {
        this.methodReturn = methodReturn;
    }

    public Set<String> getImportSet() {
        return importSet;
    }

    public void addImport(String... importClasses){
        if (importClasses != null && importClasses.length > 0){
            importSet.addAll(Arrays.asList(importClasses));
        }
    }

    public String getMethodText(){
        StringBuilder builder = new StringBuilder();
        buildJavaDoc(builder);
        buildSpringMapping(builder);
        buildSwaggerDoc(builder);
        buildMethodContext(builder);
        return builder.toString();
    }

    /**
     *
     */
    private void buildJavaDoc(StringBuilder builder){
        builder.append("/**\n")
                .append("* ").append(apiRemark).append(" \n")
                .append("*/\n");
    }

    private void buildSpringMapping(StringBuilder builder) {
        if ("GET".equals(httpMethod)){
            builder.append("@").append("GetMapping(\"").append(httpUrl).append("\")\n");
            importSet.add("org.springframework.web.bind.annotation.GetMapping");
        } else if ("POST".equals(httpMethod)){
            builder.append("@").append("PostMapping(\"").append(httpUrl).append("\")\n");
            importSet.add("org.springframework.web.bind.annotation.PostMapping");
        }else if ("PUT".equals(httpMethod)){
            builder.append("@").append("PutMapping(\"").append(httpUrl).append("\")\n");
            importSet.add("org.springframework.web.bind.annotation.PutMapping");
        }else if ("DELETE".equals(httpMethod)){
            builder.append("@").append("DeleteMapping(\"").append(httpUrl).append("\")\n");
            importSet.add("org.springframework.web.bind.annotation.DeleteMapping");
        }
    }

    private void buildSwaggerDoc(StringBuilder builder){
        builder.append("@ApiOperation(value = \"").append(apiName).append("\", notes = \"").append(apiRemark).append("\")\n");
        importSet.add("io.swagger.annotations.ApiOperation");
    }

    private void buildMethodContext(StringBuilder builder){
        if (methodVisibility != null && methodVisibility.length() > 0){
            builder.append(methodVisibility).append(" ");
        }
        builder.append(methodReturn).append(" ").append(methodName).append(" ").append("(").append(buildMethodParam()).append("){\n");
        builder.append(buildMethodBody()).append("\n");
        builder.append("}\n");

        importSet.add("com.tongtu.cyber.common.api.vo.Result");
    }

    private String buildMethodParam(){
       return "";
    }

    private String buildMethodBody(){
        StringBuilder builder = new StringBuilder();
        builder.append("  ").append("return ").append(modelName).append("Service.").append(methodName).append("();");
        return builder.toString();
    }

}
